//==============================================================================
#include "CNetworkPacket.h"

//==============================================================================
CNetworkPacket::CNetworkPacket() {
    memset(Header, 0, CNETWORKPACKET_HEADER_SIZE);
    memset(Data, 0, CNETWORKPACKET_DATA_SIZE);

    memset(Buffer, 0, CNETWORKPACKET_DATA_SIZE);

    HeaderBytesRead = 0;
    DataBytesRead   = 0;

    Message         = 0;
    DataSize        = 0;
}

//==============================================================================
unsigned int CNetworkPacket::ReadHeader(int Socket) {
    if(HeaderBytesRead == CNETWORKPACKET_HEADER_SIZE) return 0;

    int BytesRead = NET2_TCPRead(Socket, (char*)Buffer, CNETWORKPACKET_HEADER_SIZE - HeaderBytesRead);

    //Probably Disconnect
    if(BytesRead < 0) {
        return 0;
    }

    memcpy(Header, Buffer + HeaderBytesRead, CNETWORKPACKET_HEADER_SIZE - HeaderBytesRead);

    HeaderBytesRead += BytesRead;

    if(HeaderBytesRead == CNETWORKPACKET_HEADER_SIZE) {
        char DataSizeBuffer[4];

        Message = Header[0];

        DataSizeBuffer[0] = Header[1];
        DataSizeBuffer[1] = Header[2];
        DataSizeBuffer[2] = Header[3];
        DataSizeBuffer[3] = Header[4];
        DataSizeBuffer[4] = '\0';

        DataSize = atoi(DataSizeBuffer);

        //printf("Read Header: %d %d\n", Message, DataSize);
    }

    return BytesRead;
}

//-----------------------------------------------------------------------------
unsigned int CNetworkPacket::ReadData(int Socket) {
    if(DataBytesRead == DataSize) return 0;

    int BytesRead = NET2_TCPRead(Socket, (char*)Data, DataSize - DataBytesRead);

    //Probably Disconnect
    if(BytesRead < 0) {
        return 0;
    }

    DataBytesRead += BytesRead;

    if(DataBytesRead == DataSize) {
        //printf("Read Data: %s %d %s\n", (char*)Data, BytesRead, SDL_GetError());
    }

    return BytesRead;
}

//-----------------------------------------------------------------------------
bool CNetworkPacket::NeedsHeader() {
    return (HeaderBytesRead != CNETWORKPACKET_HEADER_SIZE);
}

//-----------------------------------------------------------------------------
bool CNetworkPacket::IsReady() {
    return (HeaderBytesRead > 0 && HeaderBytesRead == CNETWORKPACKET_HEADER_SIZE && DataBytesRead == DataSize);
}

//==============================================================================
char CNetworkPacket::GetMessageID() {
    return Message;
}

//-----------------------------------------------------------------------------
Uint8* CNetworkPacket::GetData() {
    return Data;
}

//-----------------------------------------------------------------------------
Uint32 CNetworkPacket::GetDataSize() {
    return DataSize;
}

//-----------------------------------------------------------------------------
std::string CNetworkPacket::GetDataString() {
    std::string Return = (char*)Data;

    return Return;
}

//==============================================================================
